package cn.stylefeng.guns.modular.school.service.impl;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.StudentGuardian;
import cn.stylefeng.guns.modular.school.mapper.StudentGuardianMapper;
import cn.stylefeng.guns.modular.school.model.params.StudentGuardianParam;
import cn.stylefeng.guns.modular.school.model.result.StudentGuardianResult;
import cn.stylefeng.guns.modular.school.service.StudentGuardianService;
import cn.stylefeng.guns.sys.core.constant.state.ManagerStatus;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 学生监护人信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-11-27
 */
@Service
public class StudentGuardianServiceImpl extends ServiceImpl<StudentGuardianMapper, StudentGuardian> implements StudentGuardianService {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(StudentGuardianParam param) {
        // 判断账号是否重复
        User theUser = userService.getByAccount(param.getTel());
        if (theUser != null) {
            throw new ServiceException(BizExceptionEnum.USER_ALREADY_REG);
        }

        StudentGuardian entity = getEntity(param);
        this.save(entity);

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("description", "JWT");
        Role role = roleService.getOne(roleQueryWrapper);
        String pwd = param.getTel();
        String salt = ShiroKit.getRandomSalt(5);
        String password = ShiroKit.md5(pwd, salt);
        User user = new User();
        user.setAccount(param.getTel());
        user.setName(param.getName());
        user.setPassword(password);
        user.setSalt(salt);
        user.setStatus(ManagerStatus.OK.getCode());
        user.setRoleId(role.getRoleId().toString());
        user.setDeptId(ShiroKit.getUser().getDeptId());
        this.userService.save(user);
    }

    @Override
    public void delete(StudentGuardianParam param) {
        StudentGuardian studentGuardian = this.getById(getKey(param));
        User user = userService.getByAccount(studentGuardian.getTel());
        userService.deleteUser(user.getUserId());

        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(StudentGuardianParam param) {
        User user = userService.getByAccount(param.getTel());
        user.setName(param.getName());
        userService.updateById(user);

        StudentGuardian oldEntity = getOldEntity(param);
        StudentGuardian newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StudentGuardianResult findBySpec(StudentGuardianParam param) {
        return null;
    }

    @Override
    public List<StudentGuardianResult> findListBySpec(StudentGuardianParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(StudentGuardianParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public StudentGuardian findByStudentIdCode(int studentId, String tel) {
        QueryWrapper<StudentGuardian> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId).eq("tel", tel);
        return this.baseMapper.selectOne(queryWrapper);
    }

    private Serializable getKey(StudentGuardianParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private StudentGuardian getOldEntity(StudentGuardianParam param) {
        return this.getById(getKey(param));
    }

    private StudentGuardian getEntity(StudentGuardianParam param) {
        StudentGuardian entity = new StudentGuardian();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
